﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using ValuationResource;
using ValuationLogic;

namespace ValuationUI
{
    /// <summary>
    /// Interaction logic for RiskViewForm.xaml
    /// </summary>
    public partial class RiskViewForm : Window
    {

        /*
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            //e.Cancel = true;
            this.Close();
        }*/

        private System.ComponentModel.BindingList<swapTrade> swapTradeList = new System.ComponentModel.BindingList<swapTrade>();
        private Thread timeUpdateThread;
        public bool modifing = false;

        public RiskViewForm()
        {
            
            InitializeComponent();
            this.currentTimeSet.SelectedDate = DateTime.Now;

            #region 注释掉便于调试调试用，正式需加上。logic设置当前时间
            Logic.setCurDate(DateTime.Now);
            #endregion
            tradeViewInit();
            timeUpdateThread = new Thread(new ThreadStart(updateTime));
            timeUpdateThread.Start();
   
        }

        private void updateTime()
        {
            while (true)
            {
                Dispatcher.Invoke((Action)delegate()
                {
                    timeUpdate();
                });
                while (modifing)
                {
                    Thread.Sleep(1000);
                }
            }
        }
        public void timeUpdate()
        {
            this.currentTimeSet.SelectedDate = DateTime.Now;
            this.hourBox.Text=((int)DateTime.Now.Hour).ToString();
            this.minuteBox.Text=((int)DateTime.Now.Minute).ToString();
            this.secondBox.Text=((int)DateTime.Now.Second).ToString();
            DateTime theTime = new DateTime((int)DateTime.Now.Year, (int)DateTime.Now.Month, (int)DateTime.Now.Day,
                int.Parse(this.hourBox.Text), int.Parse(this.minuteBox.Text), int.Parse(this.secondBox.Text));
            Logic.setCurDate(theTime);
        }
        
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {

            if (this.Visibility == Visibility.Visible && this.Owner.Visibility == Visibility.Visible)
            {
                this.Hide();
                return;
            }
            else
            {
                this.timeUpdateThread.Abort();
            }
        }
        
        public void tradeViewInit()
        {
            this.swapTradeListView.ItemsSource = null;
            //allSwapTradList = Logic.getSwaps("123");
            //FileInfo swapFile = new FileInfo("swap.swap");
            //readFile(swapFile);
            if (MainWindow.allSwapTradList == null)
            {
                return;
            }
            foreach (Swap newSwap in MainWindow.allSwapTradList)
            {
                swapTradeList.Add(new swapTrade(newSwap));
            }
             CollectionView swapTradeView = (CollectionView)CollectionViewSource.GetDefaultView(swapTradeList);
             this.swapTradeListView.ItemsSource = swapTradeView;
             
        }
        public void tradeViewUpdate(Swap theSwap)
        {
            if (MainWindow.allSwapTradList == null)
            {
                MainWindow.allSwapTradList = new List<Swap>();
            }
            MainWindow.allSwapTradList.Add(theSwap);
            swapTradeList.Add(new swapTrade(theSwap));
            CollectionView swapTradeView = (CollectionView)CollectionViewSource.GetDefaultView(swapTradeList);
            this.swapTradeListView.ItemsSource = swapTradeView;
        }
        public void updatePnL_Click()
        {
            List<Swap> newAllSwapList=new List<Swap>();
            DateTime theTime = new DateTime((int)((DateTime)currentTimeSet.SelectedDate).Year, (int)((DateTime)currentTimeSet.SelectedDate).Month, (int)((DateTime)currentTimeSet.SelectedDate).Day,
                int.Parse(this.hourBox.Text), int.Parse(this.minuteBox.Text), int.Parse(this.secondBox.Text));
           
            this.swapTradeListView.ItemsSource = null;
            int iter = 0;
            foreach (swapTrade trade in swapTradeList)
            {
                newAllSwapList.Add(swapTradeList[iter].update(MainWindow.allSwapTradList[iter]));
                //trade.update(allSwapTradList[iter]);
                //swapTradeList.Add(trade);
                iter++;
            }
            CollectionView swapTradeView = (CollectionView)CollectionViewSource.GetDefaultView(swapTradeList);
            this.swapTradeListView.ItemsSource = swapTradeView;

            /*
            int iter = 0;
            foreach (swapTrade trade in swapTradeList)
            {
                trade.update(allSwapTradList[iter]);
                iter++;
            }*/
            /*
            swapTradeList.Clear();
            swapTrade updatedSwapTrade = null;
            foreach (Swap newSwap in allSwapTradList)
            {
                updatedSwapTrade = new swapTrade(newSwap);
               
                swapTradeList.Add(new swapTrade(newSwap));
            }
            //CollectionView swapTradeView = (CollectionView)CollectionViewSource.GetDefaultView(swapTradeList);
            //this.swapTradeListView.ItemsSource = swapTradeView;
             */

        }

        private void updatePnL_Click(object sender, RoutedEventArgs e)
        {
            List<Swap> newAllSwapList = new List<Swap>();
            DateTime theTime = new DateTime((int)((DateTime)currentTimeSet.SelectedDate).Year, (int)((DateTime)currentTimeSet.SelectedDate).Month,(int)((DateTime)currentTimeSet.SelectedDate).Day,
                int.Parse(this.hourBox.Text), int.Parse(this.minuteBox.Text), int.Parse(this.secondBox.Text));
            this.swapTradeListView.ItemsSource = null;
            int iter = 0;
            foreach (swapTrade trade in swapTradeList)
            {
                newAllSwapList.Add(swapTradeList[iter].update(MainWindow.allSwapTradList[iter]));
                //trade.update(allSwapTradList[iter]);
                //swapTradeList.Add(trade);
                iter++;
            }
            CollectionView swapTradeView = (CollectionView)CollectionViewSource.GetDefaultView(swapTradeList);
            this.swapTradeListView.ItemsSource = swapTradeView;
            /*
            int iter = 0;
            foreach (swapTrade trade in swapTradeList)
            {
                trade.update(allSwapTradList[iter]);
                iter++;
            }*/
            /*
            swapTradeList.Clear();
            swapTrade updatedSwapTrade = null;
            foreach (Swap newSwap in allSwapTradList)
            {
                updatedSwapTrade = new swapTrade(newSwap);
               
                swapTradeList.Add(new swapTrade(newSwap));
            }
            //CollectionView swapTradeView = (CollectionView)CollectionViewSource.GetDefaultView(swapTradeList);
            //this.swapTradeListView.ItemsSource = swapTradeView;
             */
            
        }

        private void currentTimeSet_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            //DateTime theTime = new DateTime((int)((DateTime)this.currentTimeSet.SelectedDate).Year, (int)((DateTime)this.currentTimeSet.SelectedDate).Month,(int)((DateTime)this.currentTimeSet.SelectedDate).Day,
                //int.Parse(this.hourBox.Text), int.Parse(this.minuteBox.Text), int.Parse(this.secondBox.Text));
            Logic.setCurDate((DateTime)this.currentTimeSet.SelectedDate);
        }

        private void hourBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (hourBox.Text.Length == 0)
            {
                return;
            }
            string numText = "";
            for (int i = 0; i < hourBox.Text.Length; i++)
            {
                if ((char)hourBox.Text[i] >= '0' && (char)hourBox.Text[i] <= '9')
                {
                    numText = numText + hourBox.Text[i].ToString();
                    if (int.Parse(numText) > 23)
                    {
                        MessageBox.Show(this, "The time is out of range");
                        this.hourBox.Text = "0";
                        return;
                    }
                }
                else
                {
                    this.hourBox.Text = "0";
                    return;
                }
            }
            this.hourBox.Text = numText;
            return;
        }

        private void minuteBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string numText = "";
            for (int i = 0; i < minuteBox.Text.Length; i++)
            {
                if ((char)minuteBox.Text[i] >= '0' && (char)minuteBox.Text[i] <= '9')
                {
                    numText = numText + minuteBox.Text[i].ToString();
                    if (int.Parse(numText) > 59)
                    {
                        MessageBox.Show(this, "The time is out of range");
                        this.minuteBox.Text = "0";
                        return;
                    }
                }
                else
                {
                    this.minuteBox.Text = "0";
                    return;
                }
            }
            this.minuteBox.Text = numText;
            return;
        }

        private void secondBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (secondBox.Text.Length == 0)
            {
                return;
            }
            string numText = "";
            for (int i = 0; i < secondBox.Text.Length; i++)
            {
                if ((char)secondBox.Text[i] >= '0' && (char)secondBox.Text[i] <= '9')
                {
                    numText = numText + secondBox.Text[i].ToString();
                    if (int.Parse(numText) > 59)
                    {
                        MessageBox.Show(this, "The time is out of range");
                        this.secondBox.Text = "0";
                        return;
                    }
                }
                else
                {
                    this.secondBox.Text = "0";
                    return;
                }
            }
            this.secondBox.Text = numText;
            return;
        }

        private void timeModifyBtn_Click_1(object sender, RoutedEventArgs e)
        {
            modifing = !modifing;
        }

  /*

        public void readFile(FileInfo swapTradeFile)
        {
            Swap newSwap = new Swap();
            FileStream myContactsFileStream = new FileStream(swapTradeFile.FullName, FileMode.Open);
            StreamReader readMyContactsFile = new StreamReader(myContactsFileStream);
            string swapTradeRead = "";
            string[] swapTradeReadArray = new string[7];
            for (int index = 0; index < 3; index++)
                swapTradeReadArray[index] = "";

            while ((swapTradeRead = readMyContactsFile.ReadLine()) != null)
            {
                if (swapTradeRead == "")
                    continue;
                swapTradeReadArray = swapTradeRead.Split('\t');
                newSwap.setCounterparty(swapTradeReadArray[0]);
                newSwap.setQuantity(double.Parse(swapTradeReadArray[1]));
                newSwap.setQuoteCode("WTI");
                newSwap.setFixedPrice(double.Parse(swapTradeReadArray[2]));
                newSwap.setPeriodStartDate(DateTime.Now.AddDays(3));
                newSwap.setPeriodEndDate(DateTime.Now.AddDays(10));
                allSwapTradList.Add(newSwap);
            }
            readMyContactsFile.Close();
            myContactsFileStream.Close();
        }
*/

    }
}
